<?php
class Wmp_Model_MailTemplate
{
	/**
	 * the zend view object (used to render the templates)
	 *
	 * @var zend_view
	 */
	private $view;
		
	protected $_template;
	
    
    /**
     * set up the mail object
     *
     */
    function __construct($emailTemplate=false)
    {
        $this->view = new Zend_View();        
        $this->setTemplate($emailTemplate);
    }
    
    function setTemplate($emailTemplate)
    {
    	$config = zend_registry::get('config');
    	if($emailTemplate){
    		$moduleTemplate =  zend_registry::get('template');
        	$scriptPath = $config->path->public->dir.'/templates/'.$moduleTemplate.'/emails';
        	
    		$template = $scriptPath.'/'.$emailTemplate.'.'.$config->views->suffix;
		        	
        	if(file_exists($template)){        		
				$this->_template = $emailTemplate;				
				$this->view->addScriptPath($scriptPath);
        	}
			else {
				$defaultTemplate = $config->path->public->dir.'/emails/'.$emailTemplate.'.'.$config->views->suffix;
				//echo $defaultTemplate; 
				if (file_exists($defaultTemplate)){
					//echo 'hello'; 
					$this->_template = $emailTemplate;
					$this->view->addScriptPath($config->path->public->dir.'/emails');
				}
			}        	
        }

        if(isset($this->_template))
        	$this->_template .=  '.'.$config->views->suffix;
    }
    
    function assign($key,$val)
    {
    	$this->view->$key  = $val;
    }
    
    function getBody($bodyType="html")
    {		
    	if(!isset($this->_template))
			return;
			
    	$this->view->bodyType = $bodyType;		
		return $this->view->render($this->_template); 	
	}   
}
